package com.aconcagua.beans {
  import com.aconcagua.factory.BeanFactory;
  
  import mx.core.IMXMLObject;
  
  [DefaultProperty("metadata")]
  public class Bean implements BeanDefinitionFactory, IMXMLObject {
    private var _id: String;
    private var _type: Class;
    private var _lazy: Boolean;
    private var _scope: String;
    private var _metadata: Array;
    
    public function Bean() {
      _scope = BeanDefinitionSupport.SCOPE_SINGLETON;
    }
    
    public function get id(): String {
      return _id;
    }
    
    public function get type(): Class {
      return _type;
    }
    
    public function set type(aType: Class): void {
      _type = aType;
    }
    
    public function get lazy(): Boolean {
      return _lazy;
    }
    
    public function set lazy(isLazy: Boolean): void {
      _lazy = isLazy;
    }
    
    public function get scope(): String {
      return _scope;
    }
    
    [Inspectable(enumeration="singleton,prototype", defaultValue="singleton")]
    public function set scope(aScope: String): void {
      _scope = aScope;
    }
    
    public function set metadata(meta: Array): void {
      _metadata = meta;
    }

    public function initialized(document:Object, id:String): void {
      _id = id;
    }
    
    public function isRegisteredIn(beanFactory: BeanFactory): Boolean {
      return beanFactory.containsBean(_id);
    }
    
    public function registerIn(beanFactory: BeanFactory): void {
      var definition: BeanDefinitionSupport = new BeanDefinitionSupport(_type, _id);

      definition.scope = _scope;
      definition.lazy = _lazy;
      definition.metadata = _metadata;

      beanFactory.registerBeanDefinition(definition);
    }
    
    public function retrieveFrom(beanFactory: BeanFactory): Object {
      return beanFactory.getBean(_id);
    }
  }
}